import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

class MinesweeperBoard {

    private final static char[] INDEX_CHAR = new char[]{
            ' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'
    };

    private char[][] board;
    private int row;
    private int col;

    MinesweeperBoard(List<String> inputBoard) {
        this.board = new char[inputBoard.size()][];
        IntStream.range(0, inputBoard.size()).forEach(i -> this.board[i] = inputBoard.get(i).toCharArray());

        this.row = this.board.length;
        this.col = this.board.length > 0 ? this.board[0].length : 0;
    }

    List<String> withNumbers() {

        IntStream.range(0, this.board.length).forEach(i -> {
                    IntStream.range(0, this.board[i].length).forEach(j -> {
                                if (this.board[i][j] == ' ') {
                                    this.board[i][j] = this.replace(i, j);
                                }
                            }
                    );
                }
        );

        List<String> result = new ArrayList<>();
        Arrays.stream(board).forEach((char[] s) -> result.add(new String(s)));

        return result;
    }

    private char replace(int x, int y) {
        int count = 0;
        for (int i = x - 1; i <= x + 1; i++) {
            for (int j = y - 1; j <= y + 1; j++) {
                if ((i >= 0 && i < this.row) && (j >= 0 && j < this.col)) {
                    if(this.board[i][j] == '*'){
                        count++;
                    }
                }
            }
        }

        return MinesweeperBoard.INDEX_CHAR[count];
    }
}